var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));

Page({

  data: {
    recycletypecontent:'请选择',
    tipText: '输入客户提供收货码',
    // 用于页面样式的
    valueIsShow: false,
    // 记录临时的值，点击按钮后再保存到对应变量中
    currentValue: '',
    firstValue: '',
    currenttid:0,
    currentsubid:0,
    secondValue: '',
    ordergoods:[],
    ispaytypeon:!0,
    paytypeval:1, //支付方式 默认线上
    imgurl:[],
    correct:!1,
    typeindex:[0,0],
    type:[["废旧"],['废旧']],
    listtype:[
        // {
        //   subtype:'废旧纸类',

        //   weight:'0.05',
        //   price:'0.6'
        // },
    ],
    // weight:0,
    // price:0,
    desc:'',
    totalprice:0,
  },
 
  onLoad(options) {
    that = this;
    let page = getCurrentPages();
    page=page[page.length-2];
    let {code,ordernum} = page.data.selectitem.apply;
    that.setData({code,ordernum},()=>{

      if (app.logininfo) {
        that.getselector()
      } else {
        util.setwatcher(app, this.afterlogin)
      }
    })
  },
  afterlogin: {
    logininfo: res => {
      that.getselector()
    } 
  },
  paytypeChange(e){
    //0线下 1线上
    console.log(e,'change');
    that.setData({paytypeval:e.detail.value==true?'1':'0'})
    
  },
  getselector(){
    api.getpaytype(that.data.ordernum,res=>{
      if(res.data.issuccess==1){
        that.setData({paytype:res.data.type,paytypeval:res.data.type=="OFF"?0:1})
      }
    })
    api.getallsublist(res=>{
    if(res.data.issuccess==1){
      let {typelist,sublist}=res.data,{type}=that.data,arr0=[],arr1=[];
      typelist.forEach(x=>{
        arr0.push({name:x.name,id:x.id})
        
      })
      sublist.forEach(x=>{
        if(arr0[0].id==x.tid){
          arr1.push(x)
        }
      })   
      type[0] = arr0;
      type[1] = arr1;
      that.setData({type})
      
      that.setData({type,sublist})
    }
  })
  },

  type(e) {
    let typeindex =  e.detail.value,{type} = this.data;
    this.setData({
      typeindex,
      recycletypecontent:`${type[0][typeindex[0]].name}-${type[1][typeindex[1]].name}`,
      currenttid:type[0][typeindex[0]].id,
      currentsubid:type[1][typeindex[1]].id,
    });

  },
  typesub(e) {
    console.log(e,'eeee');

    let {value,column} = e.detail,{type,sublist} = that.data,arr1 = [];
    if(column==0){
      sublist.forEach(x=>{
        if(x.tid==type[0][value].id)
        arr1.push(x)
      })
      type[1] = arr1;
      that.setData({type})
    }
  },
  addtype(){
    let {weight,singleprice,recycletypecontent,currenttid,currentsubid,ordergoods,listtype,totalprice}=that.data,err=''
    if(currenttid<=0||currentsubid<=0)err="请重新选择回收类型"
    if(singleprice==0||isNaN(Number(singleprice)))err="请输入回收物品的单价"
    if(weight==0||isNaN(Number(weight)))err="请输入回收物品的重量"
    if(recycletypecontent=="请选择")err="请选择回收类型"
    if(err.length>0){
      wx.showModal({
        title: '提示',
        showCancel:!1,
        content:err
      })
      return;
    }
    let typesub=recycletypecontent.split("-")[1],price =  (singleprice*weight).toFixed(2)
    listtype.push({subtype:typesub,weight,price})
    ordergoods.push({weight,price,currenttid,currentsubid})
    totalprice=Number(totalprice)+Number(price)
    that.setData({listtype,ordergoods,recycletypecontent:"请选择",price:null,singleprice:null,weight:null,totalprice})
  },
  close(e){
    let {index}=e.currentTarget.dataset,{listtype,ordergoods,totalprice}=that.data
    totalprice=Number(totalprice)-Number(listtype[index].price)
    listtype.splice(index,1)
    ordergoods.splice(index,1)
    // console.log(e.currentTarget.dataset,'eeeee');
    that.setData({listtype,ordergoods,totalprice})
  },
  onShow: function () {
    this.passwordBox = this.selectComponent('#passwordBox')
  },
  
  addimg(e){
    let { imgurl } = this.data;
    wx.chooseImage({
      count: 3-imgurl.length,
      sizeType:'compressed',
      success: function(res) {
        imgurl = imgurl.concat(res.tempFilePaths)
        that.setData({ imgurl})

      },
    })
  },

  
  input(e){
    that.setData({
      [e.currentTarget.dataset.prop]:e.detail.value
    })
  },

  navigatorTo(e) {
    let url = e.currentTarget.dataset.url 
    wx.navigateTo({
      url: url
    })
  },
  // 调用组件中的方法
  toggleValue() {
    this.setData({
      valueIsShow: !this.data.valueIsShow
    }) 
    this.passwordBox.toggleValue()
  },
  inputChange(e) {
    let currentValue = e.detail 
    console.log(e.detail)
    this.setData({
      currentValue
    })
  },
  saveInputValue() {
    var {code,currentValue}=this.data;
    if(code!=currentValue) {
      util.nocancelmodal('收货码不正确')
      return;
    }
    that.setData({correct:!0})
    // wx.navigateTo({
    //   url: '../recyclephoto/recyclephoto',
    // })
      
  },

  save(){
    wx.showLoading({
      title: '加载中',
    })
    //ts 下单时间戳 毫秒
    if(that.data.btnloading)return;
    that.setData({btnloading:!0})
    let {imgurl,desc,ordernum,ordergoods,paytypeval} = that.data,err= '',weightarr=[],pricearr=[],currenttidarr=[],currentsubidarr=[];
    // if(imgurl.length==0)err = '请上传照片'
    // if(desc.trim()=='')err = '请填写备注说明'
    if(ordergoods.length==0)err = '请添加回收物品的信息'
    if(err.length>0){
      wx.hideLoading()
      that.setData({btnloading:!1})
      wx.showModal({
        title: '提示',
        showCancel:!1,
        content:err
      })
      return;
    }
    ordergoods.forEach((x,i)=>{
      weightarr.push(parseInt(x.weight*1000))
      pricearr.push(Math.round(x.price*100))
      currenttidarr.push(x.currenttid)
      currentsubidarr.push(x.currentsubid)
    })
    weightarr=String(weightarr)
    pricearr=String(pricearr)
    currenttidarr=String(currenttidarr)
    currentsubidarr=String(currentsubidarr)
    let data = {desc,ordernum,weightarr,pricearr,currenttidarr,currentsubidarr,paytype:paytypeval}
  console.log(data,'data');
  
    if(imgurl.length>0){
      api.uploadimg({ url: 'https://chinawzhs.cn/api/upload/wx_upload', path: imgurl }, {
        success: resarr => {
          wx.hideLoading()
          console.log(resarr,'resarr');
          data['picpath'] = resarr
          that.postdata(data)
          // target['imgurl'] = oldarr.concat(resarr);
          // that.confirmsubmit(target)
        }
      })
    }else{
      that.postdata(data)

    }

    // 
  },
  postdata(data){
    // util.nocancelmodal('功能逻辑修改中',"",res=>{
    //       })
   api.recycleconfirm(data,res=>{
    that.setData({btnloading:!1})
    wx.hideLoading() 
      if(res.data.issuccess==1){
        util.nocancelmodal('提交成功！',"",res=>{
          let page = getCurrentPages();
          page=page[page.length-2];
          page.setData({needfresh:!0},()=>{
            wx.navigateBack()
          })
          })
      }else{
        util.nocancelmodal(res.data.msg)
      }
   })
  },
  // checkPassword() {
  //   this.saveInputValue()
  //   console.log('验证密码...')
  // }
})